package cn.hl.sfzy.controller;

import cn.hl.sfzy.entity.Question;
import cn.hl.sfzy.entity.Reply;
import cn.hl.sfzy.service.QuestionService;
import cn.hl.sfzy.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName QuestionController
 * @Description TODO
 * @Author yangshen
 * @Date 2024年10月25日 21:03
 * @Version 1.0
 */
@RestController
@RequestMapping("question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @GetMapping
    public Result findByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize,
            Question question){
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questions = questionService.find(question);
        PageInfo<Question> questionPageInfo = new PageInfo<>(questions);
        return Result.success(questionPageInfo);
    }
    @GetMapping("findAll")
    public Result findAll(){
        return Result.success(questionService.find(new Question()));
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") long id){
        if (questionService.delete(id)!=0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Long id){
        return Result.success(questionService.selectById(id));
    }

    @PostMapping
    public Result add(@RequestBody Question question){

        if (questionService.insert(question)!=0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
}
